package com.jt.www.aop;

import com.jt.www.enums.ResultEnum;
import com.jt.www.exception.BizException;
import com.jt.www.exception.IdempotentException;
import com.jt.www.exception.ParamException;
import com.jt.www.exception.ServiceException;
import com.jt.www.model.reps.GenericResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

/**
 * 描述：统一异常处理类
 * 类名称：GlobalDefultExceptionHandler.java
 * 作者： wangyang
 * 版本：1.0
 * 修改： 2018/7/23 15:15
 * 创建日期： 2018/7/23 15:15
 * 版权：江泰保险经纪股份有限公司
 */

@ControllerAdvice
public class GlobalDefultExceptionHandler {

    public static final Logger log = LoggerFactory.getLogger(GlobalDefultExceptionHandler.class);

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public ResponseEntity defultExcepitonHandler(HttpServletRequest request, Throwable e) {
        StringBuilder result = new StringBuilder();
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
            for (FieldError fieldErro : methodArgumentNotValidException.getBindingResult().getFieldErrors()) {
/**             result.append(fieldErro.getDefaultMessage() + "\n"); **/
                result.append(fieldErro.getDefaultMessage());
                break;
            }
            return GenericResponse.ng(result.toString());
/**         String resultStr = result.toString();
 return GenericResponse.ng(resultStr.substring(0, resultStr.length()-1)); **/
        } else if (e instanceof ConstraintViolationException) {
            ConstraintViolationException constraintViolationException = (ConstraintViolationException) e;
            for (ConstraintViolation constraintViolation : constraintViolationException.getConstraintViolations()) {

                result.append(constraintViolation.getMessage());
                break;
/**             result.append(constraintViolation.getMessage()+"\n");**/
            }
            return GenericResponse.ng(result.toString());
/**         String resultStr = result.toString();
 return GenericResponse.ng(resultStr.substring(0, resultStr.length()-1));**/
        } else if (e instanceof BizException || e instanceof ParamException) {
            return GenericResponse.ng(e.getMessage());
        } else if (e instanceof ServiceException) {
            log.error("ServiceException", e);
            return GenericResponse.ng(ResultEnum.SERVER_ERROR.getMsg());
        }
        if (e instanceof IdempotentException) {
            IdempotentException idempotentException = (IdempotentException) e;
            String msg = idempotentException.getMsg();
            return GenericResponse.ng(msg);
        } else {
            log.error("URI:{},  Throwable", request.getRequestURI(), e);
            e.printStackTrace();
            return GenericResponse.ng(ResultEnum.SERVER_ERROR.getMsg());
        }
    }

}
